package com.hz.ruoyilgorder.controller;

import com.hz.ruoyilgorder.api.service.LgCar;
import com.hz.ruoyilgorder.api.service.SubColdSome;
import com.hz.ruoyilgorder.service.LgInboundOrderService;
import com.hz.ruoyilgorder.service.LgOutOrderService;
import com.hz.ruoyilgorder.service.LgSellerOrderService;
import com.hz.ruoyilgorder.utils.ResultJSON;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author yzw
 * @Date 2025/3/12 19:01
 * @注释
 */
@RestController
@RequestMapping("/ec")
public class EchartsController {

    @Autowired
    private LgInboundOrderService lgInboundOrderService;
    @Autowired
    private LgOutOrderService lgOutOrderService;
    @Autowired
    private LgCar lgCar;
    @Autowired
    private LgSellerOrderService lgSellerOrderService;
    @Autowired
    private SubColdSome subColdSome;

    @GetMapping("/getEchartsData")
    //@GlobalTransactional//分布式事务
    public ResponseEntity<Map<String, Object>> getEchartsData() {
        Map<String, Object> stats = new HashMap<>();
        //获得入库订单数
        long count = lgInboundOrderService.count();
        //获得出库订单数
        long count1 = lgOutOrderService.count();
        //获取司机
        int totalDriver = lgCar.getTotalDriver();
        //获取车辆
        int totalVehicle = lgCar.getTotalVehicle();
        stats.put("orders", count+count1);
        stats.put("drivers", totalDriver);
        stats.put("users", 458);
        stats.put("vehicles", totalVehicle);

        Map<String, String> titles = new HashMap<>();
        titles.put("orders", "订单总数");
        titles.put("drivers", "司机总数");
        titles.put("users", "用户总数");
        titles.put("vehicles", "车辆总数");
        Map<String, Object> result = new HashMap<>();
        result.put("stats", stats);
        result.put("titles", titles);
        return ResponseEntity.ok(result);

    }

    @GetMapping("/getdate")
    //@GlobalTransactional//分布式事务
    public ResponseEntity<Map<String, Object>> getdate(
            @RequestParam String startDate, @RequestParam String endDate) {
        // 解析字符串为 LocalDate
        LocalDate startDateParsed = LocalDate.parse(startDate);
        LocalDate endDateParsed = LocalDate.parse(endDate);

        // 转换为 LocalDateTime
        LocalDateTime startDateTime = startDateParsed.atStartOfDay(); // 2025-03-12 00:00:00
        LocalDateTime endDateTime = endDateParsed.atTime(23, 59, 59); // 2025-03-12 23:59:59
        // 获取每一天的订单量
        List<Integer> dailyOrders = lgSellerOrderService.getDailyOrders(startDateTime, endDateTime);
        // 构建返回的数据
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyOrders); // 返回每天的订单量
        return ResponseEntity.ok(result);

    }

    @GetMapping("/getColdAdress")
    //@GlobalTransactional//分布式事务
    public ResultJSON getColdAdress() {
        return ResultJSON.success(subColdSome.getColdAdress());
    }



}
